/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mtt.rms;

import mtt.menu.MenuController;
import mtt.menu.MenuModel;
import mtt.rms.events.recordstores.marks.RecordStoreMarks;
import mtt.rms.events.recordstores.marks.events.AddMarkRecordStoreListenerEvent;
import mtt.rms.events.recordstores.marks.events.GetNumMarkRecordsEvent;
import mtt.rms.events.recordstores.marks.events.GetVectorMarksEvent;
import mtt.rms.events.recordstores.marks.events.RemoveMarkEvent;
import mtt.rms.events.recordstores.marks.events.StoreMarkEvent;
import mtt.rms.events.recordstores.marks.events.UpdateMarkEvent;
import mtt.rms.recordstores.calendar.RecordStoreCalendar;
import mtt.rms.recordstores.calendar.events.AddEventToRmsEvent;
import mtt.rms.recordstores.calendar.events.DeleteEventFromRmsEvent;
import mtt.rms.recordstores.calendar.events.GetCurrentWeekEvent;
import mtt.rms.recordstores.calendar.events.GetNumCalendarRecordsEvent;
import mtt.rms.recordstores.calendar.events.UpdateEventInRmsEvent;
import mtt.rms.recordstores.options.RecordStoreOptions;
import mtt.rms.recordstores.options.events.GetApplicationOptionsEvent;
import mtt.rms.recordstores.options.events.UpdateApplicationOptionsEvent;
import hmvc.j2me.component.IComponent;
import hmvc.j2me.controller.AController;
import hmvc.j2me.events.IEvent;

/**
 * 
 * 
 * @author jens.meiss
 */
public class RecordStoreController extends AController {

	private IComponent recordStoreCalendar;
	private IComponent recordStoreMarks;
	private IComponent recordStoreOptions;

	public void init() {
		recordStoreCalendar = getComponent().addChildComponent(
				new RecordStoreCalendar());
		recordStoreMarks = getComponent().addChildComponent(
				new RecordStoreMarks());
		recordStoreOptions = getComponent().addChildComponent(
				new RecordStoreOptions());

		getComponent().addChildComponent(new MenuController(), new MenuModel());
	}

	public void terminate() {
	}

	public void handleEvent(IEvent event) {

		boolean continueHandling = true;
		
		if (continueHandling) {
			continueHandling = delegateRecordStoreMarksEvents(event);
		}
		
		if (continueHandling) {
			continueHandling = delegateRecordStoreOptionsEvents(event);
		}
		
		if (continueHandling) {
			continueHandling = delegateRecordStoreCalendarEvents(event);
		}
		

	}

	/**
	 * 
	 * @param event
	 * @return continue handling
	 */
	private boolean delegateRecordStoreCalendarEvents(IEvent event) {
		
		if (event instanceof GetCurrentWeekEvent) {
			recordStoreCalendar.fireEventLocal(event);
			return false;
		} else if (event instanceof AddEventToRmsEvent) {
			
			recordStoreCalendar.fireEventLocal(event);
			return false;
		} else if (event instanceof UpdateEventInRmsEvent) {
			
			recordStoreCalendar.fireEventLocal(event);
			return false;
		} else if (event instanceof DeleteEventFromRmsEvent) {
			
			recordStoreCalendar.fireEventLocal(event);
			return false;
		}  else if (event instanceof GetNumCalendarRecordsEvent) {
			
			recordStoreCalendar.fireEventLocal(event);
			return false;
		}
		
		
		
		return true;
	}

	/**
	 * 
	 * @param event
	 * @return continue handling
	 */
	private boolean delegateRecordStoreOptionsEvents(IEvent event) {
		
		if (event instanceof GetApplicationOptionsEvent) {
			
			recordStoreOptions.fireEventLocal(event);
			return false;
			
		} else if (event instanceof UpdateApplicationOptionsEvent) {
			
			recordStoreOptions.fireEventLocal(event);
			return false;
		}
		
		return true;
		
	}

	/**
	 * 
	 * @param event
	 * @return continue handling
	 */
	private boolean delegateRecordStoreMarksEvents(IEvent event) {

		if (event instanceof GetVectorMarksEvent) {

			recordStoreMarks.fireEventLocal(event);
			return false;

		} else if (event instanceof StoreMarkEvent) {

			recordStoreMarks.fireEventLocal(event);
			return false;

		} else if (event instanceof RemoveMarkEvent) {

			recordStoreMarks.fireEventLocal(event);
			return false;

		} else if (event instanceof UpdateMarkEvent) {

			recordStoreMarks.fireEventLocal(event);
			return false;

		} else if (event instanceof AddMarkRecordStoreListenerEvent) {

			recordStoreMarks.fireEventLocal(event);
			return false;

		} else if (event instanceof GetNumMarkRecordsEvent) {

			recordStoreMarks.fireEventLocal(event);
			return false;

		}

		
		
		return true;

	}

	public void registerEvents() {

		/*
		 * Listen to all RecordStoreMarks-Events
		 */
		register(GetVectorMarksEvent.class, this);
		register(StoreMarkEvent.class, this);
		register(RemoveMarkEvent.class, this);
		register(UpdateMarkEvent.class, this);
		register(AddMarkRecordStoreListenerEvent.class, this);
		register(GetNumMarkRecordsEvent.class, this);
		
		/*
		 * Listen to all RecordStoreOptions-Events
		 */
		register(GetApplicationOptionsEvent.class, this);
		register(UpdateApplicationOptionsEvent.class, this);
		
		/*
		 * TODO: Listen to all RecordStoreCalendar-Events 
		 */
		register(GetCurrentWeekEvent.class, this);
		register(AddEventToRmsEvent.class, this);
		register(UpdateEventInRmsEvent.class, this);
		register(DeleteEventFromRmsEvent.class, this);
		register(GetNumCalendarRecordsEvent.class, this);
		

	}

}
